<?php
class drive
{
    static private $myconn;
    static private function connect()
    {
        try
        {
            if(!self::$myconn)
            {
                $user = $_SERVER['DB_USER'];
                $pass = $_SERVER['DB_PASS'];
                $myconn = mysql_connect("127.0.0.1:3306", $user, $pass);
                $db = mysql_select_db('seoapp');
                mysql_query("set names 'utf8'");
            }
        }
        catch(Exception $e)
        {
            throw $e;
        }
        return true;
    }

    static public function get_one_row($sql)
    {
        try
        {
            self::connect();
            $result = mysql_query($sql);
            $num_rows = mysql_num_rows($result);
            $row = mysql_fetch_array($result);
            $num_fields = mysql_num_fields($result);
            $keys = array_keys($result);
            $values = array_values($row);
            for ($i=0; $i < $num_fields; $i++)
            {
                $r[$keys[2*$i+1]] = $values[2*$i+1];
            }

        }
        catch(Exception $e)
        {
            throw $e;
        }
        return $r;
    }
    static public function get_one_value($sql)
    {
        try
        {
            self::connect();
            $result = mysql_query($sql);
            if($result=='')
            {
            		return 0;
            }
            $row = mysql_fetch_array($result);
            return $row[0];

        }
        catch(Exception $e)
        {
            throw $e;
        }
        return $row[0];
    }
    static public function get_all_rows($sql)
    {
        $resultarray=array();
        try
        {
            self::connect();
            $result = mysql_query($sql);
            if($result==false)
            {
            		return $resultarray;
            }
            $num_rows = mysql_num_rows($result);
            $row = mysql_fetch_array($result);
            $num_fields = mysql_num_fields($result);
            $keys = @array_keys($row);
            for ($rownumber=0; $rownumber < $num_rows; $rownumber++)
            {
                $values = array_values($row);
                for ($i=0; $i < $num_fields; $i++)
                {
                    $resultarray[$rownumber][$keys[2*$i+1]] = $values[2*$i+1];
                }
                $row = mysql_fetch_array($result);
            }
        }
        catch(Exception $e)
        {
            throw $e;
        }
        return $resultarray;
    }
    static public function get($sql)
    {
        try
        {
            self::connect();
            $result = mysql_query($sql);
        }
        catch(Exception $e)
        {
            throw $e;
        }
        return $result;
    }
    static public function exc($sql)
    {
        try
        {
            self::connect();
            $result = mysql_query($sql);
        }
        catch(Exception $e)
        {
            throw $e;
        }
    }
}
?>
